package net.lijingye.education.controller;

import net.lijingye.education.entity.Course;
import net.lijingye.education.entity.Source;
import net.lijingye.education.entity.Topic;
import net.lijingye.education.exception.DaoException;
import net.lijingye.education.exception.ObjectNotExistsException;
import net.lijingye.education.response.BaseResponse;
import net.lijingye.education.response.ErrorResponse;
import net.lijingye.education.response.SuccessResponse;
import net.lijingye.education.response.meta.ObjectNotExistsMeta;
import net.lijingye.education.service.CourseService;
import net.lijingye.education.util.CommonUtil;
import net.lijingye.education.vo.PageVO;
import net.lijingye.education.vo.SourceLiteVO;
import net.lijingye.education.vo.TopicLiteVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by LijingYe on 2016/3/27.
 */
@RestController
public class CourseController extends BaseController {
    @Autowired
    private CourseService courseService;

    @RequestMapping(path = "/courses/{courseId}/books", method = RequestMethod.GET)
    public BaseResponse getCourseBooks(@PathVariable String courseId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            List<Source> bookList = courseService.getCourseBookListById(courseId);
            List<SourceLiteVO> sourceLiteVOList = new ArrayList<>();
            bookList.forEach(book -> {
                sourceLiteVOList.add(new SourceLiteVO(book));
            });
            return CommonUtil.splitPage(sourceLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(path = "/courses/{courseId}/exams", method = RequestMethod.GET)
    public BaseResponse getCourseExams(@PathVariable String courseId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            List<Source> examList = courseService.getCourseExamListById(courseId);
            List<SourceLiteVO> sourceLiteVOList = new ArrayList<>();
            examList.forEach(exam -> {
                sourceLiteVOList.add(new SourceLiteVO(exam));
            });
            return CommonUtil.splitPage(sourceLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(path = "/courses/{courseId}/interviews", method = RequestMethod.GET)
    public BaseResponse getCourseInterviews(@PathVariable String courseId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            Course course = courseService.getCourseById(courseId);
            List<Source> interviewList = courseService.getCourseInterviewListById(courseId);
            List<SourceLiteVO> sourceLiteVOList = new ArrayList<>();
            interviewList.forEach(interview -> {
                sourceLiteVOList.add(new SourceLiteVO(interview));
            });
            return CommonUtil.splitPage(sourceLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(path = "/courses/{courseId}/difficulties", method = RequestMethod.GET)
    public BaseResponse getCourseDifficulties(@PathVariable String courseId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            Course course = courseService.getCourseById(courseId);
            List<Topic> interviewList = courseService.getCourseDifficultyListById(courseId);
            List<TopicLiteVO> topicLiteVOList = new ArrayList<>();
            interviewList.forEach(difficluty -> {
                topicLiteVOList.add(new TopicLiteVO(difficluty));
            });
            return CommonUtil.splitPage(topicLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }

    @RequestMapping(path = "/courses/{courseId}/expansions", method = RequestMethod.GET)
    public BaseResponse getCourseExpansions(@PathVariable String courseId, @RequestParam(value = "page", required = false) String page) throws DaoException {
        try {
            Course course = courseService.getCourseById(courseId);
            List<Topic> interviewList = courseService.getCourseExpansionListById(courseId);
            List<TopicLiteVO> topicLiteVOList = new ArrayList<>();
            interviewList.forEach(expansion -> {
                topicLiteVOList.add(new TopicLiteVO(expansion));
            });
            return CommonUtil.splitPage(topicLiteVOList, page, super.getPageSize());
        } catch (ObjectNotExistsException e) {
            System.out.println(e.getMessage());
            return new ErrorResponse(new ObjectNotExistsMeta());
        }
    }
}
